package com.AFH.test;

import java.util.ArrayList;

public class StartQueueThread extends Thread {

	boolean flag = false;
	boolean spStart = false;
	boolean paused = false;
	ArrayList<Integer> Queue = new ArrayList<Integer>();
	ArrayList<Sprite> sp = new ArrayList<Sprite>();
	int currentId = 0;

	public StartQueueThread() {
		super.setName("StartQueueThread");
		flag = true;
		spStart = true;
	}
	public void pauseAnimation(){
		spStart = false;
		paused=true;
	}
	
	public void resumeAnimation(){
		if(paused){
			paused=false;
			spStart=true;
		}
	}
	public void stopAnimation(){
		flag=false;
		spStart=false;
	}

	public StartQueueThread(int SQT[], Sprite sp[]) {
		for (int time : SQT) {
			Queue.add(time);
		}
		for (Sprite tsp : sp) {
			this.sp.add(tsp);
		}

		flag = true;
	}

	public void addQueue(int SQT[], Sprite sp[]) {
		if (!this.isAlive()) {
			for (int time : SQT) {
				Queue.add(time);
			}
			for (Sprite tsp : sp) {
				this.sp.add(tsp);
			}
		}
	}

	public void addSprite(int time, Sprite sp) {
		if (!this.isAlive()) {
			Queue.add(time);
			this.sp.add(sp);
		}

	}

	@Override
	public void run() {
		while (flag) {
			while (spStart) {
				if (currentId < Queue.size()) {
					sp.get(currentId).startAnimation();
					try {
						Thread.sleep(Queue.get(currentId));
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					currentId++;
				} else {
					flag = false;
					spStart = false;
				}

			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}
